﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using Web.Helper;

namespace Web.Areas.Manager.Controllers
{
    //[UserAuthorize]
    public class SalesOpportunityController : Controller
    {
       SalesOpportunity_BLL _SalesOpportunity_BLL = new SalesOpportunity_BLL();
       CustomerSource_BLL _CustomerSource_BLL = new CustomerSource_BLL();
       SaleStage_BLL _SaleStage_BLL = new SaleStage_BLL();
       Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
       Customer_BLL _Customer_BLL = new Customer_BLL();
       CustomerContact_BLL _CustomerContact_BLL = new CustomerContact_BLL();
       Quotation_BLL _Quotation_BLL = new Quotation_BLL();
        private M_ReceiveForm_BLL _M_ReceiveForm_BLL = new M_ReceiveForm_BLL();
          public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _SalesOpportunity_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
               model = model.Where(a => a.ChanceName.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }


        public virtual ActionResult Details(Guid Id)
        {
            ViewData["CustomerSource"] = _CustomerSource_BLL.Get();
            ViewData["Customer"] = _Customer_BLL.Get();

            ViewData["SaleStage"] = _SaleStage_BLL.Get();
            ViewData["Sys_Users"] = _Sys_Users_BLL.Get();
            var item = _SalesOpportunity_BLL.Get(Id);
            return View(item);
        }



        public virtual ActionResult Edit(Guid? Id, string strCompanyId)
        {
            if (_Customer_BLL.Get().Count() == 0)
            {
                Response.Write("<script>alert('请先添加客户管理中的客户信息')</script>");
                Response.End();
            }
            if (_CustomerContact_BLL.Get().Count() == 0)
            {
                Response.Write("<script>alert('请先添加客户管理中的客户联系人信息')</script>");
                Response.End();
            }
            if (_CustomerSource_BLL.Get().Count() == 0)
            {
                Response.Write("<script>alert('请先添加客户管理中的客户来源类型')</script>");
                Response.End();
            }
            if (_SaleStage_BLL.Get().Count() == 0)
            {
                Response.Write("<script>alert('请先添加销售管理中的销售阶段信息')</script>");
                Response.End();
            }
            var customer = _Customer_BLL.Get();
            var customercontact = _CustomerContact_BLL.Get();
            var customerId = customer.FirstOrDefault().Id;
            var customerIdcontactId = customercontact.FirstOrDefault().Id;
           
            if (!string.IsNullOrEmpty(strCompanyId))
            {
                customerId = new Guid(strCompanyId);
                SelectList listItem = new SelectList(customer.AsEnumerable(), "Id", "CustomerName", customerId);
                ViewData.Add("Customer", listItem);
            }
            else
            {
                SelectList listItem = new SelectList(customer.AsEnumerable(), "Id", "CustomerName");
                ViewData.Add("Customer", listItem);
            }
            customercontact = customercontact.Where(a => a.CustomerId == customerId);

            SelectList listItem2 = new SelectList(customercontact.AsEnumerable(), "Id", "Name");
            ViewData.Add("CustomerContact", listItem2);
                        
            ViewData["CustomerSource"] = _CustomerSource_BLL.Get();
            
            ViewData["Customer"] = _Customer_BLL.Get();

            ViewData["SaleStage"] = _SaleStage_BLL.Get();
            ViewData["Sys_Users"] = _Sys_Users_BLL.Get();
           
            
            var item = _SalesOpportunity_BLL.Get(Id);
            if (Id == null)
            {
                ViewData["Id"] = null;
            }
               
            else
            {
                ViewData["Id"] = Id;

            } 
           
            return View(item);
        }

        [HttpPost]
        [ValidateInput(false)]
         public virtual ActionResult Edit(Guid? Id, SalesOpportunity collection)
        {
            if (!ModelState.IsValid)
            {
                Edit(Id, "");
                return View(collection);
            }
           
            _SalesOpportunity_BLL.Save(Id, ref collection);
            return RedirectToAction("Index",MyrouteValues.Get());
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
            //var model = _SalesOpportunity_BLL.Get(Id);
            //foreach (var item in model.Quotations )
            //{
            //   foreach (var item2 in item.ContactOrders)
            //    {
            //      foreach (var item3 in item2.M_ReceiveForm)
            //      {
            //         _M_ReceiveForm_BLL.Delete(item3 .Id);
            //      }
            //    }
            //    _Quotation_BLL .Delete(item.Id);
            //}
            _SalesOpportunity_BLL.Delete(Id);
            return RedirectToAction("Index",MyrouteValues.Get());
        }
    }
}
